package com.hhtc.jsj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhtc.jsj.entity.PostOperate;
import com.hhtc.jsj.entity.User;
import com.hhtc.jsj.mapper.PostOperateMapper;
import com.hhtc.jsj.request.PostOperateRequest;
import com.hhtc.jsj.request.UserRequest;
import com.hhtc.jsj.service.PostCommentService;
import com.hhtc.jsj.service.PostLikeService;
import com.hhtc.jsj.service.PostOperateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 操作表 服务实现类
 * </p>
 *
 * @author ty
 * @since 2022-04-07
 */
@Slf4j
@Service
public class PostOperateServiceImpl extends ServiceImpl<PostOperateMapper, PostOperate> implements PostOperateService {
    @Autowired
    private PostCommentService postCommentService;
    @Autowired
    private PostLikeService postLikeService;
    @Override
    @Transactional
    public boolean add(PostOperateRequest postOperateRequest) {
        postOperateRequest.setId(IdWorker.getId());
        postOperateRequest.setCreatedTime(System.currentTimeMillis());
        postOperateRequest.setUpdatedTime(System.currentTimeMillis());
        PostOperate postOperate = BeanUtil.copyProperties(postOperateRequest, PostOperate.class);
        return this.save(postOperate);
    }

    @Override
    @Transactional
    public boolean remove(PostOperateRequest postOperateRequest) {
        Long id = postOperateRequest.getId();
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean update(PostOperateRequest postOperateRequest) {
        postOperateRequest.setUpdatedTime(System.currentTimeMillis());
        PostOperate postOperate = BeanUtil.copyProperties(postOperateRequest, PostOperate.class);
        return this.updateById(postOperate);
    }

    @Override
    public PostOperateRequest getById(PostOperateRequest postOperateRequest) {
        Long id = postOperateRequest.getId();
        PostOperate postOperate = this.getById(id);
        if (postOperate != null) {
            return BeanUtil.copyProperties(postOperate, PostOperateRequest.class);
        }
        return null;
    }

    @Override
    public List<PostOperateRequest> queryList(PostOperateRequest postOperateRequest) {
        QueryWrapper<PostOperate> queryWrapper = new QueryWrapper<>();
        this.buildWrapper(queryWrapper, postOperateRequest);
        List<PostOperate> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return BeanUtil.copyToList(list, PostOperateRequest.class);
    }

    @Override
    public Page queryPage(PostOperateRequest postOperateRequest) {
        Page<PostOperate> page = new Page<>();
        QueryWrapper<PostOperate> wrapper = new QueryWrapper<PostOperate>();
        buildWrapper(wrapper, postOperateRequest);
        Page<PostOperate> pageResult = this.page(page, wrapper);
        return pageResult;
    }
    private void buildWrapper(QueryWrapper<PostOperate> wrapper, PostOperateRequest param) {
        log.info("buildPageWrapper param :{}", param);
        Long id = param.getId();
        if (id != null) {
            wrapper.eq("id", id);
        }
        Integer operateObjectType = param.getOperateObjectType();
        if (operateObjectType != null) {
            wrapper.eq("operate_object_type", operateObjectType);
        }
        Long operateRelationId = param.getOperateRelationId();
        if (operateRelationId != null) {
            wrapper.eq("operate_relation_id", operateRelationId);
        }
        Integer operateType = param.getOperateType();
        if (operateType !=null) {
            wrapper.eq("operate_type", operateType);
        }
        String operateReason = param.getOperateReason();
        if (StringUtils.isNotBlank(operateReason)) {
            wrapper.eq("operate_reason", operateReason);
        }
    }
}
